package com.cloud.lan.service.impl;

import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import cn.hutool.json.JSONUtil;

@Service
public class RedisService {

    @Resource(name = "redisTemplate0")
    RedisTemplate<String, String> template;

    @Resource
    RedisTemplate<String, String> commonRedis;

    public void set(String key, Object value) {
        template.opsForValue().set(key, JSONUtil.toJsonStr(value));
    }

    public void set(String key, Object value, long timeout, TimeUnit unit) {
        template.opsForValue().set(key, JSONUtil.toJsonStr(value), timeout, unit);
    }

    public Boolean setEx(final String key, final long timeout, final String value) {
        return template.execute(
                (RedisCallback<Boolean>)
                        redisConnection -> {
                            byte[] keys = getRedisMasterSerializer().serialize(key);
                            byte[] values = getRedisMasterSerializer().serialize(value);
                            return redisConnection.setEx(keys, timeout, values);
                        });
    }

    protected RedisSerializer<String> getRedisMasterSerializer() {
        RedisSerializer<String> redisSerializer = getRedisTemplate().getStringSerializer();
        return redisSerializer;
    }

    /**
     * 向通道发送消息的方法
     *
     * @param channel
     * @param message
     */
    public void sendChannelMess(String channel, String message) {
        template.convertAndSend(channel, message);
    }

    public long getLong(String key) {
        String value = template.opsForValue().get(key);
        if (value == null) {
            return 0;
        }
        return Long.valueOf(value);
    }

    public String getString(String key) {
        String value = template.opsForValue().get(key);
        return value;
    }

    public String get(final String key) {
        return template.execute(
                (RedisCallback<String>)
                        redisConnection -> {
                            RedisSerializer<String> serializer = getRedisSlaveSerializer();
                            byte keys[] = serializer.serialize(key);
                            byte values[] = redisConnection.get(keys);
                            if (values == null) {
                                return null;
                            }
                            String value = serializer.deserialize(values);
                            return value;
                        });
    }

    public void delete(String key) {
        template.delete(key);
    }

    /**
     * 获取从库的字符串序列化对象
     *
     * @return
     */
    protected RedisSerializer<String> getRedisSlaveSerializer() {
        RedisSerializer<String> redisSerializer = getRedisTemplate().getStringSerializer();
        return redisSerializer;
    }

    public RedisTemplate<String, String> getRedisTemplate() {
        return template;
    }

    /**
     * 存放共通数据
     * @author ErKang
     * @return java.lang.String
     * 2022/4/23 17:35
     */
    public void setCommonRedis(String key, Object value) {
        commonRedis.opsForValue().set(key, JSONUtil.toJsonStr(value));
    }

    /**
     * 获取共通数据
     * @author ErKang
     * @return java.lang.String
     * 2022/4/23 17:35
     */
    public String getCommonRedis(final String key) {
        return commonRedis.execute(
                (RedisCallback<String>)
                        redisConnection -> {
                            RedisSerializer<String> serializer = getRedisSlaveSerializer();
                            byte keys[] = serializer.serialize(key);
                            byte values[] = redisConnection.get(keys);
                            if (values == null) {
                                return null;
                            }
                            String value = serializer.deserialize(values);
                            return value;
                        });
    }

    /**
     * 存放带过期时间共通数据
     * @author ErKang
     * @return java.lang.String
     * 2022/4/23 17:35
     */
    public Boolean setCommonEx(final String key, final long timeout, final String value) {
        return commonRedis.execute(
                (RedisCallback<Boolean>)
                        redisConnection -> {
                            byte[] keys = getRedisMasterSerializer().serialize(key);
                            byte[] values = getRedisMasterSerializer().serialize(value);
                            return redisConnection.setEx(keys, timeout, values);
                        });
    }


    public boolean lock(String key,long timeout,TimeUnit timeUnit){
        Boolean r = template.boundValueOps(key).setIfAbsent("1");
        template.expire(key,timeout,timeUnit);
        return r;
    }

    public void unlock(String key){
        template.delete(key);
    }
}

